<!DOCTYPE html>
<html>

<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
Line chart for Quantified Self meeting in London
http://www.meetup.com/LondonQS/events/68812532/ 

Author silvano.luciani@gmail.com (Silvano Luciani)
-->

  <head>
    <title>Line Chart</title>
    <script type="text/javascript"
      src='https://www.google.com/jsapi?autoload={
        "modules": [{
          "name": "visualization",
          "version": "1",
          "packages": ["controls"]
        }]
      }'>
    </script>
    <script
      src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script type="text/javascript" src="date-en-US.js"></script>
  </head>
  <body>    
    <div id="line_chart_div"></div>
    <script type="text/javascript">
    $.getJSON('data.json', function(data) {
      
      // The data table for our chart
      var dataTable = new google.visualization.DataTable();
      dataTable.addColumn('datetime', 'Posting Date');
      dataTable.addColumn('number', 'Comments');
      dataTable.addColumn('number', 'Plusones');
      dataTable.addColumn('number', 'Reshares');
      
      // Extract the data we want to visualize
      $.each(data.items, function(key, val) {
        var date = Date.parseExact(
            val.published, 'yyyy-MM-ddTHH:mm:ss.sssZ');
        // Line below because the date is not always parsed correctly
        if(date) {
          dataTable.addRow([
            date,
            val.object.replies.totalItems,
            val.object.plusoners.totalItems,
            val.object.resharers.totalItems,
          ]);
        }
      });
      
      // Wrapper for the line chart that shows the engagement on posts
      var wrapper = new google.visualization.ChartWrapper({
        'chartType': 'LineChart',
        'dataTable': dataTable,
        'options': {
          'title': 'Engagement on posts',
          'width': 700,
          'height': 300,
          'animation':{
            'duration': 400,
            'easing': 'out'
          },
        },
        'containerId': 'line_chart_div',
      });
      
      // Draw the chart
      wrapper.draw();    
    });
    </script>
  </body>
</html>